extends CenterContainer

var data = null

var area_id = null

onready var sprite_box = $PanelContainer/VBoxContainer/sprites

const MapSprteItem = preload("res://tool/page/map/mapView/spriteArea/MapSprteItem.tscn")

func _ready():
	pass 



func set_data(data):
	self.data = data


func set_area_id(area_id):
	self.area_id = area_id
	
	var map_sprte_area = DataSource.get_data_by_one(DataSource.map_sprite_area, area_id)
	
	if map_sprte_area:
		$PanelContainer/VBoxContainer/name.text = "【%s】%s" % [area_id, map_sprte_area["areaDesc"]]
	else:
		$PanelContainer/VBoxContainer/name.text = "【%s】" % area_id
	
	var map_sprte_item_list = DataSource.get_data_by_list(DataSource.map_sprite_area_item, area_id, "pokeAreaId")
	
	for child in sprite_box.get_children():
		child.queue_free()
	
	for sprite in map_sprte_item_list:
		var ui = MapSprteItem.instance()
		sprite_box.add_child(ui)
		
		ui.set_data(sprite)
		
		ui.connect("edit", self, "_on_edit")
		ui.connect("delete", self, "_on_delete")
		
		
func _on_cancel_button_up():
	MainTool.getUILayer().pop_ui()


func _on_areaInfo_button_up():
	MainTool.getUILayer().pop_ui()
	var ui = MainTool.getUILayer().add_ui("res://tool/page/map/mapView/spriteArea/modal/SpriteAreaModal.tscn", 0, false)
	ui.set_area_id(area_id)



func _on_addSprite_button_up():
	var map_sprte_area = DataSource.get_data_by_one(DataSource.map_sprite_area, area_id)
	
	if !map_sprte_area:
		MainTool.toast_info("请先设置好区域信息后，才可添加精灵分布")
		return
		
	MainTool.getUILayer().pop_ui()
	var ui = MainTool.getUILayer().add_ui("res://tool/page/map/mapView/spriteArea/modal/AddAreaSpriteModal.tscn", 0, false)
	ui.set_edit_data(null, area_id)


func _on_delete(data):
	var poke_id = data["pokeId"]
	var poke_info = DataSource.get_spirit_by_id(poke_id)
	MainTool.confirm("你是否确认删除该区域%s分布" % poke_info["name"], self, "_on_del_ok", null, data)
	pass


func _on_del_ok(data):
	
	MainTool.getUILayer().pop_ui()
	DataSource.delete_data_by_id(DataSource.map_sprite_area_item, data["id"])
	
	MainTool.toast_info("删除分布成功！")
	


func _on_edit(data):
	MainTool.getUILayer().pop_ui()
	var ui = MainTool.getUILayer().add_ui("res://tool/page/map/mapView/spriteArea/modal/AddAreaSpriteModal.tscn", 0, false)
	ui.set_edit_data(data, area_id)
